'use strict';

/**
 * @ngdoc function
 * @name webApp.controller:FooterCtrl
 * @description
 * # FooterCtrl
 * Controller of the webApp
 */


angular.module('webApp')
  .controller('footer', function ($scope,$rootScope,$location,jqLite,$timeout,cookie,popBox,promiseFooter,$animate,$localStorage) {
    $rootScope.textBoxShow = true;
    $scope.$on('$stateChangeSuccess',function(){
      //格式化评论框
      $scope.$emit('footShow',{title: '写评论',action: 'hide'});
      $timeout(function(){
        if(jqLite.$('footer')[0].attributes.length == 5){
          jqLite.$('footer')[0].attributes[4].value = '';
        }
      });
      //首页，报告，服务
      jqLite.$('footerBarAdd').removeClass('addClick');
      if($location.$$path == '/root/home' ||
        $location.$$path == '/root/report' ||
        $location.$$path == '/root/server' ||
        $location.$$path == '/root/moreContent' ||
          $location.$$path == '/root/moreReport'
      ){
        $scope.footerAllData = [
          {
            data: '文章',
            img: 'homeRouteBar1',
            url: 'root.home'
          },
          {
            data: '报告',
            img: 'homeRouteBar2',
            url: 'root.report'
          },
          {
            data: '数据库',
            img: 'homeRouteBar3',
            url: 'root.home'
          },
          {
            data: '服务',
            img: 'homeRouteBar4',
            url: 'root.server'
          }
        ];
        //尾部链接
        $scope.urlBack = function(index){
          if(index == 2){
            window.location = 'http://www.shippingdata.cn/free/rf/index/?from=timeline&isappinstalled=0';
          }
        };
        //尾部宽度
        $scope.footerDataWidth = 1 / $scope.footerAllData.length * 100 + '%';
        //增加样式
        $timeout(function(){
          $scope.footerAllData[1].img = 'homeRouteBar2';
          if($location.$$path == '/root/home'){
            angular.forEach(jqLite.$('footerBarAdd'),function(obj,index){
              if(obj.childNodes[3].innerText == '文章'){
                jqLite.$('footerBarAdd').eq(index).addClass('addClick');
              }
            });
          }
          else if($location.$$path == '/root/report'){
            angular.forEach(jqLite.$('footerBarAdd'),function(obj,index){
              if(obj.childNodes[3].innerText == '报告'){
                jqLite.$('footerBarAdd').eq(index).addClass('addClick');
              }
            });
          }
          else if($location.$$path == '/root/server'){
            angular.forEach(jqLite.$('footerBarAdd'),function(obj,index){
              if(obj.childNodes[3].innerText == '服务'){
                jqLite.$('footerBarAdd').eq(index).addClass('addClick');
              }
            });
          }
        });
      }
      //正文页
      else if($location.$$path == '/root/mainText'){
        //判断是否点赞
        var mainStar;
        $scope.footerAllData = [
          {
            data: '',
            img: 'mainTextEdit',
            url: 'root.mainText'
          },
          {
            data: '',
            img: 'mainTextStar',
            url: 'root.mainText'
          },
          {
            data: '',
            img: 'mainTextShare',
            url: 'root.mainText'
          }
        ];
        $rootScope.$on('star',function(ele,data){
          mainStar = data.data;
          if($location.$$path == '/root/mainText'){
            if(data.data){
              $scope.footerAllData[1].img = 'mainTextStarSelect';
            }
            else{
              $scope.footerAllData[1].img = 'mainTextStar';
            }
          }
        });
        //尾部宽度
        $scope.footerDataWidth = 1 / $scope.footerAllData.length * 100 + '%';
        //收藏
        $scope.urlBack = function(index){
          if(index == 1 && cookie.getCookie('access_token')){
            if(!mainStar){
              promiseFooter.stars().then(function(){
                popBox.showConfirm('提示','收藏成功！');
                $scope.footerAllData[1].img = 'mainTextStarSelect';
              });
            }
            else if(mainStar){
              promiseFooter.deleteStars().then(function(){
                popBox.showConfirm('提示','取消收藏成功！');
                $scope.footerAllData[1].img = 'mainTextStar';
              });
            }
            mainStar = !mainStar;
          }
          else if(index == 1){
            popBox.showConfirm('警告','请先登录！');
          }
          //评论
          else if(index == 0){
            $scope.$emit('footShow',{title: '写评论',action: 'show'});
          }
        };
      }
      else if($location.$$path == '/root/textCommunicate'){

      }
    });
    //显示隐藏评论框
    $scope.$on('footShow',function(ele,data){
      if(data.action == 'show'){
        $rootScope.textBoxShow = false;
        $rootScope.coverShow = true;
        //写入信息
        $scope.commitTitle = data.title;
        //显示dom
        $scope.commitBox = true;
        //动画
        $timeout(function(){
          $animate['addClass'](jqLite.$('footer'),'footerShow');
        });
      }
      else if(data.action == 'hide'){
        $rootScope.textBoxShow = true;
        $rootScope.coverShow = false;
        $scope.commitBox = false;
        $timeout(function(){
          $animate['removeClass'](jqLite.$('footer'),'footerShow');
        });
      }
    });
    //关闭评论框
    $scope.close = function(){
      $scope.$emit('footShow',{title: '写评论',action: 'hide'});
    };
    //评论发送功能
    $scope.sendOut = function(){
      if(cookie.getCookie('access_token')){
        if(jqLite.$('commentText')[0].value != ''){
          if($scope.commitTitle == '写评论'){
            promiseFooter.textMainComments(jqLite.$('commentText')[0].value,'','',$localStorage.flag).then(function(data){
              popBox.showConfirm('提示','评论成功！');
              $scope.$emit($location.$$path,{data: data,action: '写评论'});
              $scope.$emit('footShow',{title: '',action: 'hide'});
            });
          }
          else if($scope.commitTitle == '写回复'){
            promiseFooter.replay(jqLite.$('commentText')[0].value,'','',$localStorage.flag).then(function(data){
              popBox.showConfirm('提示','回复成功！');
              $scope.$emit($location.$$path,{data: data,action: '写回复'});
              $scope.$emit('footShow',{title: '',action: 'hide'});
            });
          }
        }
        else{
          popBox.showConfirm('警告','请填写内容！');
        }
      }
      else{
        if(jqLite.$('commentText')[0].value != ''){
          popBox.showPopup($scope,'请输入验证码','');
        }
        else{
          popBox.showConfirm('警告','请填写内容！');
        }
      }
    };
});
